<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="邀请码" prop="yqcode">
        <el-input v-model="dataForm.yqcode" placeholder="邀请码"></el-input>
      </el-form-item>
      <el-form-item label="1已使用 2 未使用" prop="state">
        <el-input v-model="dataForm.state" placeholder="1已使用 2 未使用"></el-input>
      </el-form-item>
      <el-form-item label="绑定客户ID" prop="bindUserId">
        <el-input v-model="dataForm.bindUserId" placeholder="绑定客户ID"></el-input>
      </el-form-item>
      <el-form-item label="创建者ID" prop="createUserId">
        <el-input v-model="dataForm.createUserId" placeholder="创建者ID"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="" prop="bindTime">
        <el-input v-model="dataForm.bindTime" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          yqcode: '',
          state: '',
          bindUserId: '',
          createUserId: '',
          createTime: '',
          bindTime: ''
        },
        dataRule: {
          yqcode: [
            { required: true, message: '邀请码不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '1已使用 2 未使用不能为空', trigger: 'blur' }
          ],
          bindUserId: [
            { required: true, message: '绑定客户ID不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建者ID不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          bindTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.baseyqcode.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.yqcode = data.baseyqcode.yqcode
                this.dataForm.state = data.baseyqcode.state
                this.dataForm.bindUserId = data.baseyqcode.bindUserId
                this.dataForm.createUserId = data.baseyqcode.createUserId
                this.dataForm.createTime = data.baseyqcode.createTime
                this.dataForm.bindTime = data.baseyqcode.bindTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'yqcode': this.dataForm.yqcode,
              'state': this.dataForm.state,
              'bindUserId': this.dataForm.bindUserId,
              'createUserId': this.dataForm.createUserId,
              'createTime': this.dataForm.createTime,
              'bindTime': this.dataForm.bindTime
            }
            var tick = !this.dataForm.id ? API.baseyqcode.add(params) : API.baseyqcode.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
